﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApC3DProcessNodeData
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using C1.Win.C1FlexGrid;
using System;
using System.Collections.Generic;
using Tecnomatix.Engineering;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands;

internal class CApC3DProcessNodeData : IDisposable
{
  private ITxOperation m_nodeObject;
  private CApC3DProcessNodeData m_parent;
  private List<CApC3DProcessNodeData> m_children = new List<CApC3DProcessNodeData>();
  private List<CApC3DProcessNodeData> m_orderedChildren = new List<CApC3DProcessNodeData>();
  private CApC3DSnapshotNodeData m_attachedSnaphotData;
  private int m_nodeLevel;
  private double m_startTime;
  private bool m_isStep;

  internal CApC3DProcessNodeData(ITxOperation nodeObject, CApC3DProcessNodeData parent)
  {
    this.m_nodeObject = nodeObject;
    this.m_parent = parent;
    if (nodeObject != null)
      ((ITxObject) nodeObject).NameChanged += new TxObject_NameChangedEventHandler(this.nodeObject_NameChanged);
    if (this.m_parent == null)
      return;
    if (this.m_parent.NodeObject is ITxCompoundOperation nodeObject1)
      this.m_startTime = this.m_parent.StartTime + nodeObject1.GetChildOperationRelativeStartTime(nodeObject);
    this.m_parent.AddChild(this);
    this.m_nodeLevel = this.m_parent.NodeLevel + 1;
  }

  private void nodeObject_NameChanged(object sender, TxObject_NameChangedEventArgs args)
  {
    this.GridRow[0] = (object) ((ITxObject) this.NodeObject).Name;
    if (this.StepsGridRow == null || ((RowCol) this.StepsGridRow).Index == -1)
      return;
    this.StepsGridRow[0] = (object) ((ITxObject) this.NodeObject).Name;
    ((RowCol) this.StepsGridRow).Grid.AutoSizeCols();
  }

  internal ITxOperation NodeObject => this.m_nodeObject;

  internal ITxPlanningObject PlanningNodeObject
  {
    get
    {
      ITxProcessModelObject nodeObject = this.m_nodeObject as ITxProcessModelObject;
      if (!(this.m_nodeObject is ITxPlanningObject planningNodeObject) && nodeObject != null)
        planningNodeObject = nodeObject.PlanningRepresentation;
      return planningNodeObject;
    }
  }

  internal int NodeLevel => this.m_nodeLevel;

  internal double StartTime => this.m_startTime;

  internal CApC3DProcessNodeData ParentNode => this.m_parent;

  internal string FullName
  {
    get
    {
      string fullName = ((ITxObject) this.m_nodeObject).Name;
      if (this.m_parent != null)
      {
        string str = fullName + " [";
        int length = str.Length;
        for (CApC3DProcessNodeData dprocessNodeData = this.m_parent; dprocessNodeData != null; dprocessNodeData = dprocessNodeData.ParentNode)
          str = str.Insert(length, ((ITxObject) dprocessNodeData.NodeObject).Name + ":");
        fullName = str.Remove(str.Length - 1, 1) + "]";
      }
      return fullName;
    }
  }

  internal bool IsStep
  {
    get => this.m_isStep;
    set => this.m_isStep = value;
  }

  internal CApC3DSnapshotNodeData AttachedSnapshotData
  {
    get => this.m_attachedSnaphotData;
    set => this.m_attachedSnaphotData = value;
  }

  public Row GridRow { get; set; }

  public Row StepsGridRow { get; set; }

  internal void AddChild(CApC3DProcessNodeData child)
  {
    this.m_children.Add(child);
    bool flag = false;
    foreach (CApC3DProcessNodeData orderedChild in this.m_orderedChildren)
    {
      if (child.StartTime < orderedChild.StartTime)
      {
        this.m_orderedChildren.Insert(this.m_orderedChildren.IndexOf(orderedChild), child);
        flag = true;
        break;
      }
    }
    if (flag)
      return;
    this.m_orderedChildren.Add(child);
  }

  internal List<CApC3DProcessNodeData> GetChildren(bool isOrdered)
  {
    return !isOrdered ? this.m_children : this.m_orderedChildren;
  }

  public void Dispose()
  {
    ((ITxObject) this.NodeObject).NameChanged -= new TxObject_NameChangedEventHandler(this.nodeObject_NameChanged);
  }
}
